//#include<iostream>
//#include<queue>
//using namespace std;
//int main()
//{
//	int n;
//	cin >> n;
//	while (n--)
//	{
//		int x, b;
//		cin >> x >> b;
//		priority_queue<int> q1;
//		priority_queue<int,vector<int>,greater<int>> q2;
//		int i = 0;
//		int count = 0;
//		for (i; i < x; i++)
//		{
//			int m;
//			cin >> m;
//			q1.push(m);
//			q2.push(m);
//			if (m > b)
//				count++;
//		}
//		if (count == 0)
//		{
//			cout << "YES" << endl;
//		}
//		else if (count == x)
//		{
//			cout << "NO" << endl;
//		}
//		else
//		{
//
//
//			int ai = q2.top();
//			q2.pop();
//			int aj = q2.top();
//			q2.pop();
//			if (ai + aj <= b)
//				cout << "YES" << endl;
//			else
//				cout << "NO" << endl;
//		}
//	}
//}
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
#include<queue>
int gcd(int x1, int x2)
{
	int m, n,c;
	m = x2; n = x1;
	while (x2)
	{
		c = x1 % x2;
		x1 = x2;
		x2 = c;
	}
	return m * n / x1;
}
int main()
{
	int n;
	cin >> n;
	while (n--)
	{
		string s1;
		string s2;
		cin >> s1;
		cin >> s2;
		queue<char> q;
		int x1 = s1.size();
		int x2 = s2.size();
		if (x1 >= x2)
		{
			int i = 0;
			while (s1[i] != 0)
			{
				q.push(s1[i]);
				i++;
			}
			i = 0;
			int j = 0;
			while (!q.empty()&&q.front() == s2[j])
			{
				q.pop();
				j++;
				if (s2[j] == 0)
					j = 0;
			}
			if (q.empty())
			{
				if (j == 0)
				{
					cout << s1<<endl;
				}
				else
				{
					if (s2[j] == s1[0])
					{
						int m = gcd(x1, x2);
						while (m--)
						{
							cout << s1[0];
						}
						cout << endl;
					}
					else
						cout << "-1" << endl;
				}
			}
		}
		else
		{
			int i = 0;
			while (s2[i] != 0)
			{
				q.push(s2[i]);
				i++;
			}
			i = 0;
			int j = 0;
			while (!q.empty()&&q.front() == s1[j])
			{
				q.pop();
				j++;
				if (s1[j] == 0)
					j = 0;
			}
			if (q.empty())
			{
				if (j == 0)
				{
					cout << s2<<endl;
				}
				else
				{
					if (s1[j] == s2[0])
					{
						int m = gcd(x2, x1);
						while (m--)
						{
							cout << s2[0];
						}
						cout << endl;
					}
					else
						cout << "-1" << endl;
				}
			}
		}
	}
}